package com.risk.modules.rm.service.business;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.risk.common.Enums.DataEnum;
import com.risk.common.beans.JsonResult;
import com.risk.common.utils.HttpRequestUtil;
import com.risk.modules.rm.beans.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 身份证校验接口
 */
public class RiskAuditService extends BusinessService {

    private final static Logger LOG = LoggerFactory.getLogger(RiskAuditService.class);

    @Override
    public DataEnum getType() {
        return DataEnum.RiskAudit;
    }

    public String execute(String name,String idCard,String phone,String bankCard){

        //TODO 调用前，先扣费。

        Map queryMap = new HashMap();
        queryMap.put("name",name); // 姓名
        queryMap.put("identityNo",idCard); // 身份证号码
        queryMap.put("mobile",phone); // 手机号

        return apiSend(JdUrls.get(getType()).url,queryMap);
    }

    @Override
    public String resultHandle(String response,Date handleTime) {
        System.out.println("=============处理响应结果：" + response);
        JSONObject data = JSONObject.parseObject(response);

        if ("10000".equals(data.get("code"))){

            JSONObject responseResult = data.getJSONObject("result");
            JSONObject responseData = responseResult.getJSONObject("data");

            JSONObject basicInfo = responseData.getJSONObject("basicInfo");


            JsonData jsonData = new JsonData();
            jsonData.setAmount(58);
            jsonData.setScore(98);
            jsonData.setTime(handleTime);

            Temp temp = new Temp();

            Temp.UserIdentity identity = temp.getIdentity();

            identity.setAddress(basicInfo.getString("identityAddress"));
            identity.setName(basicInfo.getString("name"));
            identity.setIdCard(basicInfo.getString("identityNo"));
            identity.setAge(basicInfo.getInteger("age"));
            identity.setBirthday(basicInfo.getString("birthday"));
            identity.setMobile(basicInfo.getString("mobile"));
            identity.setMobileAddress(basicInfo.getString("mobileAddress"));
            identity.setSex(basicInfo.getString("gender"));

            jsonData.setIdentity(identity);

            JSONArray array = responseData.getJSONArray("riskItems");

            List<Temp.Risk> risks = new ArrayList<Temp.Risk>(array.size());

            Map<String,Temp.Risk> tempMap = new HashMap<String, Temp.Risk>();

            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = array.getJSONObject(i);
                Temp.Risk risk = temp.getRisk();
                risk.setTitle(obj.getString("group"));

                List<Temp.Items> items;

                if(null != tempMap.get(risk.getTitle())){
                    risk = tempMap.get(risk.getTitle());
                    items = risk.getItems();
                }else{
                    items = new ArrayList<Temp.Items>();
                    risks.add(risk);
                    tempMap.put(risk.getTitle(),risk);
                }

                Temp.Items item = temp.getItems();

                item.setIsHit("1".equals(obj.getString("isHit")));
                item.setLevel(Level.get(obj.getString("riskLevel")).value);
                item.setName(obj.getString("itemName"));

                if(item.getIsHit()) {

                    JSONArray itemDetailArray = obj.getJSONObject("itemDetail").getJSONArray("details");

                    List<Temp.ResultValue> details = new ArrayList<Temp.ResultValue>();

                    for (int j = 0; j < itemDetailArray.size(); j++) {

                        JSONArray detailsArray = itemDetailArray.getJSONArray(j);

                        for (int l = 0; l < detailsArray.size();l++) {

                            JSONObject detailObj = detailsArray.getJSONObject(l);

                            Temp.ResultValue detail = temp.getResultValue();
                            detail.setLabel(JDConstant.KEY_MAP.get(obj.getJSONObject("itemDetail").getString("detailType")).get(detailObj.getString("key")));
                            detail.setValue(detailObj.getString("value"));

                            details.add(detail);
                        }
                    }
                    item.setDetails(details);
                }
                items.add(item);

                risk.setItems(items);
            }
            jsonData.setRisks(risks);
            return JSONObject.toJSONString(jsonData);

        }
        return "";
    }
}
